package com.longrise.co.ui.mine.fragment;

import android.app.ProgressDialog;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.longrise.co.R;
import com.ly.common.base.BaseFragment;
import com.umeng.socialize.ShareAction;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.media.UMImage;
import com.umeng.socialize.media.UMWeb;
import com.umeng.socialize.utils.SocializeUtils;

import butterknife.Bind;
import butterknife.OnClick;

/**
 * Created by luoyang on 2017-7-20.
 */

public class ShareFragment extends BaseFragment {

    private static final String LOAD = "https://qcda.96520.com/zhejiang_webmap/zhejiang_chezhu";

    @Bind(R.id.head_title)
    TextView headTitle;

    @Override
    protected int getLayoutResource() {
        return R.layout.mine_share;
    }

    @Override
    public void initPresenter() {

    }

    @Override
    protected void initView() {
        setText(headTitle, "我的分享");
    }

    public static ShareFragment newInstance() {
        return new ShareFragment();
    }

    @OnClick({R.id.we_chat, R.id.friend, R.id.qq, R.id.qq_zone, R.id.head_back})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.head_back :
                _mActivity.onBackPressed();
                break;
            case R.id.we_chat:
                shareTo(SHARE_MEDIA.WEIXIN);
                break;
            case R.id.friend:
                shareTo(SHARE_MEDIA.WEIXIN_CIRCLE);
                break;
            case R.id.qq:
                shareTo(SHARE_MEDIA.QQ);
                break;
            case R.id.qq_zone:
                shareTo(SHARE_MEDIA.QZONE);
                break;
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        SocializeUtils.safeCloseDialog(umDialog);
    }

    //分享到其他应用
    private void shareTo(SHARE_MEDIA var1) {
        ShareAction action = new ShareAction(_mActivity);
        action.setPlatform(var1);

        UMWeb web = new UMWeb(LOAD);
        web.setTitle("汽车电子健康档案车主版本");//标题
        web.setDescription("汽车电子健康档案由河南省交通运输厅主管，为您提供汽车维修记录查询、维修企业信息查询、服务评价与投诉，以及交通事故快处快赔等多样化的用车信息服务。河南车主的好管家，行车路上的好帮手！");//描述
        web.setThumb(new UMImage(_mActivity, R.drawable.logo));  //缩略图

        action.withMedia(web);//附带的图片，音乐，视频等多媒体对象
        action.setCallback(umShareListener);//设置友盟分享后状态监听
        action.share();//发起分享，调起微信，QQ，微博客户端进行分享
    }

    private ProgressDialog umDialog;

    private UMShareListener umShareListener = new UMShareListener() {

        @Override
        public void onStart(SHARE_MEDIA platform) {
            if (umDialog == null) {
                umDialog = new ProgressDialog(_mActivity);
            }
            SocializeUtils.safeShowDialog(umDialog);
        }

        @Override
        public void onResult(SHARE_MEDIA platform) {
            SocializeUtils.safeCloseDialog(umDialog);
            if (platform.name().equals("WEIXIN_FAVORITE")) {
                showLongToast("收藏成功");
            } else {
                showLongToast("分享成功");
            }
        }

        @Override
        public void onError(SHARE_MEDIA platform, Throwable t) {
            SocializeUtils.safeCloseDialog(umDialog);
            showLongToast("分享失败");
            if (t != null) {
                Log.d("throw", "throw:" + t.getMessage());
            }
        }

        @Override
        public void onCancel(SHARE_MEDIA platform) {
//            UiUtil.showToast(context, platform + " 分享取消啦");
            SocializeUtils.safeCloseDialog(umDialog);
        }
    };

}
